package br.com.javamagazine;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.javamagazine.jdo.UserFoto;
import br.com.javamagazine.jdo.UserFotoDAO;

@SuppressWarnings("serial")
public class FotoImageServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("image/jpg");
		
		String path = req.getRequestURI();
		//Retira o "/foto/" do path
		UserFoto userFoto = UserFotoDAO.getUserFoto(path.substring(6));

		if (userFoto != null) {
			OutputStream out = resp.getOutputStream();
			byte[] data = userFoto.getImageData().getBytes();
			out.write(data);
			out.flush();
			out.close();
		}
	}
}
